# 2. 山脉的个数
function count_peaks(a) {
let count = 0;
for(let i=0; i<a.length; i++) {
if (i===0 && a[i] > a[i+1]) {
count++;
}
if (i===a.length-1 && a[i] > a[i-1]) {
count++;
}
if (i>0 && i<a.length-1 && a[i] > a[i-1] && a[i] > a[i+1]) {
count++;
}
}
return count;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16